package com.wyl.login.activity

import android.text.Html
import android.text.method.ScrollingMovementMethod
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.ObservableBody
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.AgreementPage
import com.wyl.base.LOGIN_AGREEMENT
import com.wyl.libbase.base.BindingActivity
import com.wyl.login.R
import com.wyl.login.databinding.AgreementActivityBinding

@Route(path = AgreementPage)
class AgreementActivity : BindingActivity<AgreementActivityBinding>() {
    override fun getLayoutId(): Int = R.layout.agreement_activity

    override fun initView() {
        binding.titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                else -> {
                }
            }
        }

        binding.textview.movementMethod = ScrollingMovementMethod.getInstance()
    }

    override fun loadData() {
        OkGo.post<BaseResponse<String>>(LOGIN_AGREEMENT)
            .converter(object : JsonConvert<BaseResponse<String>>() {})
            .adapt(ObservableBody<BaseResponse<String>>())
            .doOnSubscribe { binding.textview.text = getString(R.string.ui_loading) }
            .subscribe({
                binding.textview.text = Html.fromHtml(it.data)
            }, {
                onError(it)
                binding.textview.text = getString(R.string.ui_load_failed)
            }).toDisposables(disposables)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.bt_close -> onBackPressed()
            else -> {
            }
        }
    }
}
